package com.workspace.chb_website.controller;

import com.workspace.chb_website.bean.MetaBean;
import com.workspace.chb_website.bean.RespBean;
import com.workspace.chb_website.service.MediaService;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * 媒体文件相关控制
 */

@CrossOrigin
@RestController
@Api(tags = "图片相关接口")
public class MediaController {

    @Value("${server.port}")
    private String port;
    private final String ip;
    private final MediaService mediaService;
    private final String resourcePath;

    @Autowired
    public MediaController(MediaService mediaService) {
        this.mediaService = mediaService;
        resourcePath = "D:/lsn/chb_resource/";
        ip = "10.1.161.130";
    }

    @ApiOperation("获取所有分类名字及图片数量")
    @GetMapping(value = "/getAllImageSum")
    public RespBean getAllImageSum() {
        File file = new File(resourcePath);
        String[] imageNames = file.list();
        assert imageNames != null;
        List<Map<String, String>> re = new ArrayList<>();
        for (String name : imageNames) {
            File f = new File(resourcePath + name);
            int sum = Objects.requireNonNull(f.list()).length;
            Map<String, String> el = new HashMap<>();
            el.put("sort", name.substring(0, name.indexOf("-")));
            el.put("name", name.substring(name.indexOf("-") + 1));
            el.put("sum", String.valueOf(sum));
            re.add(el);
        }
        return new RespBean(mediaService.sortImageNames(re), new MetaBean(200, "请求成功!"));
    }

    @ApiOperation("获取所属分类的所有图片的url")
    @ApiImplicitParams({@ApiImplicitParam(name = "sort", value = "图片分类", required = true, dataType = "String")})
    @GetMapping(value = "/getImagesUrl")
    public RespBean getImagesUrl(@RequestParam String sort) {
        String imagesPath = resourcePath + mediaService.getSortName(sort);
        File file = new File(imagesPath);
        String[] imageNames = file.list();
        if (!file.exists() || imageNames == null) {
            return new RespBean("", new MetaBean(400, "未能找到资源!"));
        }
        List<String> re = new ArrayList<>();
        for (String imageName : imageNames) {
            re.add("http://" + ip + ":" + port + "/image/" + sort + "/" + imageName);
        }
        return new RespBean(re, new MetaBean(200, "请求成功!"));
    }

    @ApiIgnore
    @GetMapping(value = "/image/{sort}/{imgName}", produces = "image/jpeg")
    public void getImage(@PathVariable("sort") String sort, @PathVariable("imgName") String imgName, HttpServletResponse response) throws IOException {
        String imgPath = resourcePath + mediaService.getSortName(sort) + "/" + imgName;
        mediaService.getImage(imgPath, response);
    }
}
